package com.xinrui.sunnyday.serivce.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xinrui.sunnyday.dao.MemberDao;
import com.xinrui.sunnyday.dao.OrderDao;
import com.xinrui.sunnyday.dao.ReportDao;
import com.xinrui.sunnyday.pojo.ReportSetMeal;
import com.xinrui.sunnyday.service.ReportService;
import com.xinrui.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao; //引入会员的dao层
    @Autowired
    private OrderDao orderDao;//引入订单的dao层

    /**
     * 柱状图:查询会员注册人数
     *
     * @param months
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport(ArrayList<String> months) {
//        1.定义一个集合将查询数据库中的过去一年的每个月的会员总数(叠加)
        ArrayList<Integer> memberCount = new ArrayList<>();
//        遍历集合将里面的数据拿出来进行查询
        for (String date : months) {
            Integer count = reportDao.findMemberReportDate(date);
            memberCount.add(count);
        }
//        将日期和从数据库中查询出来的总数加到map中
        Map<String, Object> map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }

    /**
     * 饼图:查询当前套餐的预约人数
     *
     * @return
     */
    @Override
    public List<ReportSetMeal> getSetMealReport() {
        List<ReportSetMeal> list = reportDao.getSetMealReport();
        return list;
    }

    /**
     * 获取运营数据
     *
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
//        0.日期转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        1.今日的
        String today = simpleDateFormat.format(DateUtils.getToday());
//        2.本周的星期一
        String thisWeekMonday = simpleDateFormat.format(DateUtils.getThisWeekMonday());
//        3.本周的最后一天
        String sundayOfThisWeek = simpleDateFormat.format(DateUtils.getSundayOfThisWeek());
//        4.本月的第一天
        String firstDay4ThisMonth = simpleDateFormat.format(DateUtils.getFirstDay4ThisMonth());
//        5.本月的最后一天
        String lastDayOfThisMonth = simpleDateFormat.format(DateUtils.getLastDayOfThisMonth());

//        会员统计查询
        //查询今天的新增会员
        int todayNewMember = memberDao.findMemberCountByDate(today);
        //查询所有的会员
        int totalMember = memberDao.findMemberTotalCount();
        //查询本周新增的会员
        int thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //查询本月新增的会员
        int thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
//        订单统计查询
        //查询当天预约人数
        int todayOrderNumber = orderDao.findOrderCountByDate(today);
        //查询当天到诊人数
        int todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //查询本周预约人数
        int thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(thisWeekMonday, sundayOfThisWeek);
        //查询本周到诊人数
        int thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //查询本月预约人数
        int thisMonthOrderNumber = orderDao.findOrderCountBetweenDate(firstDay4ThisMonth, lastDayOfThisMonth);
        //查询本月到诊人数
        int thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);
//        热门套餐
        List<Map<String, Object>> hotSetMeal = orderDao.findHotSetmeal();

        Map<String, Object> map = new HashMap<>();
        map.put("reportDate", today);
        map.put("todayNewMember", todayNewMember);
        map.put("totalMember", totalMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("hotSetMeal", hotSetMeal);
        return map;
    }
}
